package com.jlnk.stockdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlnk.stockdemo.R;
import com.jlnk.stockdemo.utils.SPUtils;
import com.jlnk.stockdemo.utils.ToastUtils;

public class LoginActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        checkRegister();
        initView();
    }

    private void checkRegister() {
        if (SPUtils.getUsername().isEmpty()) {
            startActivity(new Intent(this, RegisterActivity.class));
            finish();
        }
    }

    private void initView() {
        EditText et_account = findViewById(R.id.et_account);
        EditText et_password = findViewById(R.id.et_password);
        Button btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = getEditText(et_account);
                String password = getEditText(et_password);
                if (SPUtils.getUsername().equals(account) && SPUtils.getPassword().equals(password)) {
                    ToastUtils.showToast("登录成功！");
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                } else {
                    ToastUtils.showToast("登录失败！账号或密码错误！");
                }
            }
        });
    }

    private String getEditText(EditText editText) {
        if (editText != null &&
                editText.getText() != null &&
                editText.getText().toString() != null &&
                !editText.getText().toString().isEmpty()) {
            return editText.getText().toString();
        }
        return "";
    }
}
